package com.example.hangzhou.welbeing.mytestdemoapp.customView;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 杭州惟君医疗科技有限公司16
 * Created by 20433 on 2018/4/17
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class MyRecyclerView extends RecyclerView {
    private View emptyView;
    public MyRecyclerView(Context context) {
        super(context);
    }

    public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public void setEmptyView(View view) {
        this.emptyView = view;
        ((ViewGroup) this.getRootView()).addView(view);
    }

    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);
        adapter.registerAdapterDataObserver(observer);
        observer.onChanged();
    }
    private AdapterDataObserver observer = new AdapterDataObserver() {
        @Override
        public void onChanged() {//设置空view原理都一样，没有数据显示空view，有数据隐藏空view
            Adapter adapter = getAdapter();
            if (adapter.getItemCount() == 0) {
                if(emptyView != null){
                    emptyView.setVisibility(VISIBLE);
                }
                MyRecyclerView.this.setVisibility(GONE);
            } else {
                if(emptyView != null){
                    emptyView.setVisibility(GONE);
                }
                MyRecyclerView.this.setVisibility(VISIBLE);
            }
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            onChanged();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            onChanged();
        }
    };

    @Override
    public void onScreenStateChanged(int screenState) {
        if(screenState == RecyclerView.SCROLL_STATE_IDLE){
            LayoutManager layoutManager =  getLayoutManager();
            int lastVisiblePosition;
            if(layoutManager instanceof GridLayoutManager){
                lastVisiblePosition = ((GridLayoutManager)layoutManager).findLastVisibleItemPosition();
            }else if(layoutManager instanceof StaggeredGridLayoutManager){
                int into[] = new int[((StaggeredGridLayoutManager)layoutManager).getSpanCount()];
               // lastVisiblePosition = findMax(into);
            }else{
                lastVisiblePosition = ((LinearLayoutManager)layoutManager).findLastVisibleItemPosition();
            }
        }
     //   super.onScreenStateChanged(screenState);
    }
}
